
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Enum 模块 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="pattern-matching.html" />
    
    
    <link rel="prev" href="collections.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        <li class="header">第一部分：基础</li>
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="basics.html">
            
                <a href="basics.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="collections.html">
            
                <a href="collections.html">
            
                    
                    集合
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4" data-path="enum.html">
            
                <a href="enum.html">
            
                    
                    Enum 模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="pattern-matching.html">
            
                <a href="pattern-matching.html">
            
                    
                    模式匹配
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="control-structures.html">
            
                <a href="control-structures.html">
            
                    
                    控制语句
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="functions.html">
            
                <a href="functions.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="pipe-operator.html">
            
                <a href="pipe-operator.html">
            
                    
                    管道操作符
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    模块(Module)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="mix.html">
            
                <a href="mix.html">
            
                    
                    Mix
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="sigils.html">
            
                <a href="sigils.html">
            
                    
                    魔符(Sigil)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="documentation.html">
            
                <a href="documentation.html">
            
                    
                    文档模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="comprehensions.html">
            
                <a href="comprehensions.html">
            
                    
                    推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="strings.html">
            
                <a href="strings.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="date-time.html">
            
                <a href="date-time.html">
            
                    
                    日期和时间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="mix-tasks.html">
            
                <a href="mix-tasks.html">
            
                    
                    自定义Mix任务
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="iex-helpers.html">
            
                <a href="iex-helpers.html">
            
                    
                    IEx辅助函数
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第二部分：高级</li>
        
        
    
        <li class="chapter " data-level="2.1" data-path="../advanced/erlang.html">
            
                <a href="../advanced/erlang.html">
            
                    
                    和Erlang互操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="../advanced/error-handling.html">
            
                <a href="../advanced/error-handling.html">
            
                    
                    错误处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="../advanced/escripts.html">
            
                <a href="../advanced/escripts.html">
            
                    
                    可执行文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="../advanced/concurrency.html">
            
                <a href="../advanced/concurrency.html">
            
                    
                    并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="../advanced/otp-concurrency.html">
            
                <a href="../advanced/otp-concurrency.html">
            
                    
                    OTP并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.6" data-path="../advanced/otp-supervisors.html">
            
                <a href="../advanced/otp-supervisors.html">
            
                    
                    OTP Supervisors
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.7" data-path="../advanced/otp-distribution.html">
            
                <a href="../advanced/otp-distribution.html">
            
                    
                    OTP 分布式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.8" data-path="../advanced/metaprogramming.html">
            
                <a href="../advanced/metaprogramming.html">
            
                    
                    元编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.9" data-path="../advanced/umbrella-projects.html">
            
                <a href="../advanced/umbrella-projects.html">
            
                    
                    Umbrella Projects
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.10" data-path="../advanced/typespec.html">
            
                <a href="../advanced/typespec.html">
            
                    
                    Specifications and types
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.11" data-path="../advanced/behaviours.html">
            
                <a href="../advanced/behaviours.html">
            
                    
                    行为
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.12" data-path="../advanced/gen-stage.html">
            
                <a href="../advanced/gen-stage.html">
            
                    
                    GenStage
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.13" data-path="../advanced/protocols.html">
            
                <a href="../advanced/protocols.html">
            
                    
                    协议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="2.14" data-path="../advanced/nerves.html">
            
                <a href="../advanced/nerves.html">
            
                    
                    Nerves
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第三部分：ECTO</li>
        
        
    
        <li class="chapter " data-level="3.1" data-path="../ecto/basics.html">
            
                <a href="../ecto/basics.html">
            
                    
                    Basics
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="../ecto/changesets.html">
            
                <a href="../ecto/changesets.html">
            
                    
                    Changesets
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="../ecto/associations.html">
            
                <a href="../ecto/associations.html">
            
                    
                    关联关系
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="../ecto/querying.html">
            
                <a href="../ecto/querying.html">
            
                    
                    查询
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第四部分：专题</li>
        
        
    
        <li class="chapter " data-level="4.1" data-path="../specifics/plug.html">
            
                <a href="../specifics/plug.html">
            
                    
                    Plug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="../specifics/eex.html">
            
                <a href="../specifics/eex.html">
            
                    
                    嵌入的 Elixir(EEx)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="../specifics/ets.html">
            
                <a href="../specifics/ets.html">
            
                    
                    Erlang 项式存储(ETS)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="../specifics/mnesia.html">
            
                <a href="../specifics/mnesia.html">
            
                    
                    Mnesia 数据库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="../specifics/debugging.html">
            
                <a href="../specifics/debugging.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    
        
        <li class="header">第五部分：程序库</li>
        
        
    
        <li class="chapter " data-level="5.1" data-path="../libraries/guardian.html">
            
                <a href="../libraries/guardian.html">
            
                    
                    Guardian(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="../libraries/poolboy.html">
            
                <a href="../libraries/poolboy.html">
            
                    
                    Poolboy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="../libraries/benchee.html">
            
                <a href="../libraries/benchee.html">
            
                    
                    Benchee
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="../libraries/bypass.html">
            
                <a href="../libraries/bypass.html">
            
                    
                    Bypass
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="../libraries/distillery.html">
            
                <a href="../libraries/distillery.html">
            
                    
                    Distillery(基础)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="../libraries/stream-data.html">
            
                <a href="../libraries/stream-data.html">
            
                    
                    StreamData
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >Enum 模块</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <p>&#x4E00;&#x4E9B;&#x679A;&#x4E3E;&#x96C6;&#x5408;&#x5143;&#x7D20;&#x7684;&#x7B97;&#x6CD5;&#x3002;</p>
<h2 id="enum">Enum</h2>
<p><code>Enum</code> &#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E86;&#x8D85;&#x8FC7;70&#x4E2A;&#x64CD;&#x4F5C;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x7684;&#x51FD;&#x6570;&#x3002;&#x6211;&#x4EEC;&#x5728;<a href="../collections">&#x4E0A;&#x4E00;&#x8282;</a>&#x5B66;&#x5230;&#x7684;&#x96C6;&#x5408;&#x7C7B;&#x578B;&#x4E2D;&#x9664;&#x4E86;&#x5143;&#x7EC4;&#xFF08;tuple&#xFF09;&#x4E4B;&#x5916;&#x90FD;&#x662F;&#x679A;&#x4E3E;&#x7C7B;&#x578B;&#x3002;</p>
<p>&#x8FD9;&#x8282;&#x8BFE;&#x7A0B;&#x6211;&#x4EEC;&#x53EA;&#x4F1A;&#x8986;&#x76D6; Enum &#x6A21;&#x5757;&#x7684;&#x4E00;&#x90E8;&#x5206;&#x51FD;&#x6570;&#xFF0C;&#x4E0D;&#x8FC7;&#x6211;&#x4EEC;&#x968F;&#x65F6;&#x90FD;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x53BB;&#x4E86;&#x89E3;&#x3002;
&#x8BA9;&#x6211;&#x4EEC;&#x5728; IEx &#x91CC;&#x505A;&#x4E2A;&#x5C0F;&#x8BD5;&#x9A8C;&#x3002;</p>
<pre><code class="lang-elixir">iex
iex&gt; Enum.__info__(:functions) |&gt; Enum.each(fn({function, arity}) -&gt;
...&gt;   IO.puts &quot;#{function}/#{arity}&quot;
...&gt; end)
all?/1
all?/2
any?/1
any?/2
at/2
at/3
...
</code></pre>
<p>&#x901A;&#x8FC7;&#x4E0A;&#x9762;&#x7684;&#x547D;&#x4EE4;&#xFF0C;&#x5F88;&#x660E;&#x663E;&#x80FD;&#x770B;&#x5230;&#x6211;&#x4EEC;&#x6709;&#x5F88;&#x591A;&#x51FD;&#x6570;&#xFF0C;&#x800C;&#x4E14;&#x8FD9;&#x4E9B;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x5E08;&#x51FA;&#x6709;&#x540D;&#x3002;
&#x96C6;&#x5408;&#x5728;&#x51FD;&#x6570;&#x5F0F;&#x7F16;&#x7A0B;&#x5360;&#x6709;&#x6838;&#x5FC3;&#x5730;&#x4F4D;&#xFF0C;&#x7ED3;&#x5408; Elixir &#x7684;&#x5176;&#x4ED6;&#x7279;&#x6027;&#xFF0C;&#x5B83;&#x53EF;&#x4EE5;&#x8D4B;&#x4E88;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#x7684;&#x80FD;&#x529B;&#x3002;</p>
<p>&#x8981;&#x60F3;&#x4E86;&#x89E3;&#x5168;&#x90E8;&#x7684;&#x51FD;&#x6570;&#xFF0C;&#x8BF7;&#x8BBF;&#x95EE;&#x5B98;&#x65B9;&#x7684; <a href="https://hexdocs.pm/elixir/Enum.html" target="_blank"><code>Enum</code></a> &#x6587;&#x6863;&#x3002;&#x800C;&#x8981;&#x60F3;&#x4E86;&#x89E3;&#x60F0;&#x6027;&#x679A;&#x4E3E;&#xFF08;lazy enumeration&#xFF09;&#xFF0C;&#x8BBF;&#x95EE; <a href="https://hexdocs.pm/elixir/Stream.html" target="_blank"><code>Stream</code></a> &#x6A21;&#x5757;&#x3002;</p>
<h3 id="all">all?</h3>
<p>&#x4F7F;&#x7528; <code>all?</code> &#x4EE5;&#x53CA;&#x5927;&#x90E8;&#x5206; <code>Enum</code> &#x51FD;&#x6570;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x6211;&#x4EEC;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x6765;&#x4F5C;&#x7528;&#x5230;&#x8981;&#x64CD;&#x4F5C;&#x7684;&#x96C6;&#x5408;&#x4E0A;&#x3002;&#x53EA;&#x6709;&#x5F53;&#x51FD;&#x6570;&#x5728;&#x6240;&#x6709;&#x7684;&#x5143;&#x7D20;&#x4E0A;&#x90FD;&#x8FD4;&#x56DE; <code>true</code> &#x7684;&#x65F6;&#x5019;&#xFF0C;<code>all?</code> &#x624D;&#x4F1A;&#x8FD4;&#x56DE; <code>true</code>&#xFF0C;&#x5426;&#x5219;&#x7ED3;&#x679C;&#x5C31;&#x662F; <code>false</code>&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Enum.all?([<span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>], <span class="hljs-keyword">fn</span>(s) -&gt; String.length(s) == <span class="hljs-number">3</span> <span class="hljs-keyword">end</span>)
<span class="hljs-keyword">false</span>
iex&gt; Enum.all?([<span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>], <span class="hljs-keyword">fn</span>(s) -&gt; String.length(s) &gt; <span class="hljs-number">1</span> <span class="hljs-keyword">end</span>)
<span class="hljs-keyword">true</span>
</code></pre>
<h3 id="any">any?</h3>
<p>&#x548C;&#x4E0A;&#x9762;&#x4E0D;&#x540C;&#xFF0C;&#x53EA;&#x8981;&#x6709;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#x88AB;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x8FD4;&#x56DE; <code>true</code>&#xFF0C;<code>any?</code> &#x5C31;&#x4F1A;&#x8FD4;&#x56DE; <code>true</code>&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Enum.any?([<span class="hljs-string">&quot;foo&quot;</span>, <span class="hljs-string">&quot;bar&quot;</span>, <span class="hljs-string">&quot;hello&quot;</span>], <span class="hljs-keyword">fn</span>(s) -&gt; String.length(s) == <span class="hljs-number">5</span> <span class="hljs-keyword">end</span>)
<span class="hljs-keyword">true</span>
</code></pre>
<h3 id="chunkevery2">chunk_every/2</h3>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x628A;&#x4F60;&#x7684;&#x96C6;&#x5408;&#x62C6;&#x5206;&#x6210;&#x5C0F;&#x7684;&#x5206;&#x7EC4;&#xFF0C;<code>chunk_every/2</code> &#x5C31;&#x662F;&#x4F60;&#x8981;&#x627E;&#x7684;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.chunk_every([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>], <span class="hljs-number">2</span>)
[[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>], [<span class="hljs-number">3</span>, <span class="hljs-number">4</span>], [<span class="hljs-number">5</span>, <span class="hljs-number">6</span>]]
</code></pre>
<p><code>chunk_every/2</code> &#x8FD8;&#x6709;&#x5176;&#x4ED6;&#x9009;&#x9879;&#xFF0C;&#x5728;&#x8FD9;&#x91CC;&#x4E0D;&#x6DF1;&#x5165;&#x4ECB;&#x7ECD;&#x3002;&#x5982;&#x679C;&#x611F;&#x5174;&#x8DA3;&#xFF0C;&#x524D;&#x5F80; <a href="https://hexdocs.pm/elixir/Enum.html#chunk_every/4" target="_blank"><code>chunk_every/4</code></a> &#x7684;&#x5B98;&#x65B9;&#x6587;&#x6863;&#x53BB;&#x4E86;&#x89E3;&#x3002;</p>
<h3 id="chunkby">chunk_by</h3>
<p>&#x5982;&#x679C;&#x4E0D;&#x6309;&#x7167;&#x6570;&#x91CF;&#x5206;&#x7EC4;&#xFF08;&#x6BCF;&#x7EC4;&#x7684;&#x5143;&#x7D20;&#x6570;&#x91CF;&#x76F8;&#x540C;&#xFF09;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>chunk_by</code> &#x65B9;&#x6CD5;&#x3002;&#x5B83;&#x63A5;&#x53D7;&#x4E00;&#x4E2A;&#x679A;&#x4E3E;&#x503C;&#x548C;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x4F5C;&#x4E3A;&#x53C2;&#x6570;&#xFF0C;&#x5982;&#x679C;&#x51FD;&#x6570;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x53D8;&#x4E86;&#xFF0C;&#x65B0;&#x7684;&#x5206;&#x7EC4;&#x5C31;&#x4ECE;&#x8FD9;&#x91CC;&#x5F00;&#x59CB;&#x521B;&#x5EFA;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.chunk_by([<span class="hljs-string">&quot;one&quot;</span>, <span class="hljs-string">&quot;two&quot;</span>, <span class="hljs-string">&quot;three&quot;</span>, <span class="hljs-string">&quot;four&quot;</span>, <span class="hljs-string">&quot;five&quot;</span>], <span class="hljs-keyword">fn</span>(x) -&gt; String.length(x) <span class="hljs-keyword">end</span>)
[[<span class="hljs-string">&quot;one&quot;</span>, <span class="hljs-string">&quot;two&quot;</span>], [<span class="hljs-string">&quot;three&quot;</span>], [<span class="hljs-string">&quot;four&quot;</span>, <span class="hljs-string">&quot;five&quot;</span>]]
iex&gt; Enum.chunk_by([<span class="hljs-string">&quot;one&quot;</span>, <span class="hljs-string">&quot;two&quot;</span>, <span class="hljs-string">&quot;three&quot;</span>, <span class="hljs-string">&quot;four&quot;</span>, <span class="hljs-string">&quot;five&quot;</span>, <span class="hljs-string">&quot;six&quot;</span>], <span class="hljs-keyword">fn</span>(x) -&gt; String.length(x) <span class="hljs-keyword">end</span>)
[[<span class="hljs-string">&quot;one&quot;</span>, <span class="hljs-string">&quot;two&quot;</span>], [<span class="hljs-string">&quot;three&quot;</span>], [<span class="hljs-string">&quot;four&quot;</span>, <span class="hljs-string">&quot;five&quot;</span>], [<span class="hljs-string">&quot;six&quot;</span>]]
</code></pre>
<h3 id="mapevery">map_every</h3>
<p>&#x6709;&#x65F6;&#x5019;&#x628A;&#x96C6;&#x5408;&#x5206;&#x7EC4;&#x5E76;&#x4E0D;&#x80FD;&#x6EE1;&#x8DB3;&#x6211;&#x4EEC;&#x7684;&#x9700;&#x6C42;&#x3002;&#x8FD9;&#x65F6;&#x5019; <code>map_every/3</code> &#x5728;&#x4FEE;&#x6539;&#x96C6;&#x5408;&#x4E2D;&#x7279;&#x5B9A;&#x5143;&#x7D20;&#x7684;&#x65F6;&#x5019;&#x4F1A;&#x975E;&#x5E38;&#x6709;&#x7528;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># Apply function every three items</span>
iex&gt; Enum.map_every([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>, <span class="hljs-number">8</span>], <span class="hljs-number">3</span>, <span class="hljs-keyword">fn</span> x -&gt; x + <span class="hljs-number">1000</span> <span class="hljs-keyword">end</span>)
[<span class="hljs-number">1001</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1004</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1007</span>, <span class="hljs-number">8</span>]
</code></pre>
<h3 id="each">each</h3>
<p>&#x6709;&#x65F6;&#x5019;&#x9700;&#x8981;&#x904D;&#x5386;&#x67D0;&#x4E2A;&#x96C6;&#x5408;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x60F3;&#x4EA7;&#x751F;&#x65B0;&#x7684;&#x503C;&#xFF08;&#x4E0D;&#x628A;&#x51FD;&#x6570;&#x7684;&#x904D;&#x5386;&#x8C03;&#x7528;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#xFF09;&#xFF0C;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>each</code>&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.each([<span class="hljs-string">&quot;one&quot;</span>, <span class="hljs-string">&quot;two&quot;</span>, <span class="hljs-string">&quot;three&quot;</span>], <span class="hljs-keyword">fn</span>(s) -&gt; IO.puts(s) <span class="hljs-keyword">end</span>)
one
two
three
<span class="hljs-symbol">:ok</span>
</code></pre>
<p><strong>&#x6CE8;&#x610F;</strong>&#xFF1A;<code>each</code> &#x51FD;&#x6570;&#x4F1A;&#x8FD4;&#x56DE;&#x539F;&#x5B50; <code>:ok</code></p>
<h3 id="map">map</h3>
<p>&#x5982;&#x679C;&#x9700;&#x8981;&#x628A;&#x6267;&#x884C;&#x7ED3;&#x679C;&#x505A;&#x4E3A;&#x4E00;&#x4E2A;&#x65B0;&#x96C6;&#x5408;&#x8FD4;&#x56DE;&#x7684;&#x8BDD;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<code>map</code> &#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.map([<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-keyword">fn</span>(x) -&gt; x - <span class="hljs-number">1</span> <span class="hljs-keyword">end</span>)
[-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>]
</code></pre>
<h3 id="min">min</h3>
<p><code>min/1</code> &#x5728;&#x96C6;&#x5408;&#x4E2D;&#x627E;&#x5230;&#x6700;&#x5C0F;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.min([<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">1</span>])
-<span class="hljs-number">1</span>
</code></pre>
<p><code>min/2</code> &#x4E5F;&#x4E00;&#x6837;&#xFF0C;&#x4F46;&#x662F;&#x5B83;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;&#x6307;&#x5B9A;&#x8BA1;&#x7B97;&#x6700;&#x5C0F;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.min([], <span class="hljs-keyword">fn</span> -&gt; <span class="hljs-symbol">:foo</span> <span class="hljs-keyword">end</span>)
<span class="hljs-symbol">:foo</span>
</code></pre>
<h3 id="max">max</h3>
<p><code>max/1</code> &#x8FD4;&#x56DE;&#x96C6;&#x5408;&#x4E2D;&#x6700;&#x5927;&#x7684;&#x503C;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.max([<span class="hljs-number">5</span>, <span class="hljs-number">3</span>, <span class="hljs-number">0</span>, -<span class="hljs-number">1</span>])
<span class="hljs-number">5</span>
</code></pre>
<p><code>max/2</code> &#x4E5F;&#x4E00;&#x6837;&#xFF0C;&#x800C;&#x4E14;&#x50CF; <code>min/2</code> &#x4E00;&#x6837;&#xFF0C;&#x5B83;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x533F;&#x540D;&#x51FD;&#x6570;&#x6307;&#x5B9A;&#x8BA1;&#x7B97;&#x6700;&#x5927;&#x503C;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.max([], <span class="hljs-keyword">fn</span> -&gt; <span class="hljs-symbol">:bar</span> <span class="hljs-keyword">end</span>)
<span class="hljs-symbol">:bar</span>
</code></pre>
<h3 id="filter">filter</h3>
<p><code>filter/2</code> &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5E2E;&#x6211;&#x4EEC;&#x8FC7;&#x6EE4;&#x96C6;&#x5408;&#xFF0C;&#x53EA;&#x7559;&#x4E0B;&#x80FD;&#x662F;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x7684;&#x51FD;&#x6570;&#x8FD4;&#x56DE;<code>true</code>&#x7684;&#x90A3;&#x4E9B;&#x5143;&#x7D20;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Enum.filter([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>], <span class="hljs-keyword">fn</span>(x) -&gt; rem(x, <span class="hljs-number">2</span>) == <span class="hljs-number">0</span> <span class="hljs-keyword">end</span>)
[<span class="hljs-number">2</span>, <span class="hljs-number">4</span>]
</code></pre>
<h3 id="reduce">reduce</h3>
<p>&#x4F7F;&#x7528; <code>reduce/3</code>&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x628A;&#x96C6;&#x5408;&#x4E0D;&#x65AD;&#x8BA1;&#x7B97;&#xFF0C;&#x6700;&#x7EC8;&#x5F97;&#x5230;&#x4E00;&#x4E2A;&#x503C;&#x3002;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x63D0;&#x4F9B;&#x4E00;&#x4E2A;&#x53EF;&#x9009;&#x7684;&#x7D2F;&#x52A0;&#x503C;&#xFF08;&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#x662F; <code>10</code>&#xFF09;&#xFF0C;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x7D2F;&#x52A0;&#x503C;&#xFF0C;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x503C;&#x4F1A;&#x88AB;&#x4F7F;&#x7528;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Enum.reduce([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-number">10</span>, <span class="hljs-keyword">fn</span>(x, acc) -&gt; x + acc <span class="hljs-keyword">end</span>)
<span class="hljs-number">16</span>
iex&gt; Enum.reduce([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>], <span class="hljs-keyword">fn</span>(x, acc) -&gt; x + acc <span class="hljs-keyword">end</span>)
<span class="hljs-number">6</span>
iex&gt; Enum.reduce([<span class="hljs-string">&quot;a&quot;</span>,<span class="hljs-string">&quot;b&quot;</span>,<span class="hljs-string">&quot;c&quot;</span>], <span class="hljs-string">&quot;1&quot;</span>, <span class="hljs-keyword">fn</span>(x,acc)-&gt; x &lt;&gt; acc <span class="hljs-keyword">end</span>)
<span class="hljs-string">&quot;cba1&quot;</span>
</code></pre>
<h3 id="sort">sort</h3>
<p>&#x5BF9;&#x96C6;&#x5408;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF0C;Elixir &#x63D0;&#x4F9B;&#x4E86;&#x4E24;&#x4E2A; <code>sort</code> &#x51FD;&#x6570;&#x6765;&#x5E2E;&#x5FD9;&#x3002;&#x7B2C;&#x4E00;&#x4E2A;&#x4F7F;&#x7528; Elixir &#x9ED8;&#x8BA4;&#x7684;&#x6392;&#x5E8F;&#x89C4;&#x5219;&#x8FDB;&#x884C;&#x6392;&#x5E8F;&#xFF1A;</p>
<p><code>sort/1</code> &#x4F7F;&#x7528;&#x4E86; Erlang &#x7684; <a href="http://erlang.org/doc/reference_manual/expressions.html#term-comparisons" target="_blank">&#x6570;&#x636E;&#x6BD4;&#x8F83;&#x89C4;&#x5219;</a> &#x6765;&#x51B3;&#x5B9A;&#x6392;&#x5E8F;&#x7684;&#x987A;&#x5E8F;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.sort([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, -<span class="hljs-number">1</span>, <span class="hljs-number">4</span>])
[-<span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]

iex&gt; Enum.sort([<span class="hljs-symbol">:foo</span>, <span class="hljs-string">&quot;bar&quot;</span>, Enum, -<span class="hljs-number">1</span>, <span class="hljs-number">4</span>])
[-<span class="hljs-number">1</span>, <span class="hljs-number">4</span>, Enum, <span class="hljs-symbol">:foo</span>, <span class="hljs-string">&quot;bar&quot;</span>]
</code></pre>
<p>&#x53E6;&#x5916; <code>sort/2</code> &#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x81EA;&#x5DF1;&#x63D0;&#x4F9B;&#x6392;&#x5E8F;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-elixir"><span class="hljs-comment"># with our function</span>
iex&gt; Enum.sort([%{<span class="hljs-symbol">:val</span> =&gt; <span class="hljs-number">4</span>}, %{<span class="hljs-symbol">:val</span> =&gt; <span class="hljs-number">1</span>}], <span class="hljs-keyword">fn</span>(x, y) -&gt; x[<span class="hljs-symbol">:val</span>] &gt; y[<span class="hljs-symbol">:val</span>] <span class="hljs-keyword">end</span>)
[%{<span class="hljs-symbol">val:</span> <span class="hljs-number">4</span>}, %{<span class="hljs-symbol">val:</span> <span class="hljs-number">1</span>}]

<span class="hljs-comment"># without</span>
iex&gt; Enum.sort([%{<span class="hljs-symbol">:count</span> =&gt; <span class="hljs-number">4</span>}, %{<span class="hljs-symbol">:count</span> =&gt; <span class="hljs-number">1</span>}])
[%{<span class="hljs-symbol">count:</span> <span class="hljs-number">1</span>}, %{<span class="hljs-symbol">count:</span> <span class="hljs-number">4</span>}]
</code></pre>
<h3 id="uniq">uniq</h3>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>uniq/1</code> &#x6765;&#x5220;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x91CD;&#x590D;&#x5143;&#x7D20;&#xFF1A;</p>
<pre><code class="lang-elixir">iex&gt; Enum.uniq([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">1</span>])
[<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]
</code></pre>
<h3 id="uniqby">uniq_by</h3>
<p><code>uniq_by/2</code> &#x4E5F;&#x53EF;&#x4EE5;&#x7528;&#x6765;&#x5220;&#x9664;&#x96C6;&#x5408;&#x4E2D;&#x7684;&#x91CD;&#x590D;&#x5143;&#x7D20;&#xFF0C;&#x53EA;&#x662F;&#x5B83;&#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x81EA;&#x5B9A;&#x4E49;&#x7684;&#x51FD;&#x6570;&#x6765;&#x4F5C;&#x552F;&#x4E00;&#x6027;&#x6BD4;&#x8F83;&#x3002;</p>
<pre><code class="lang-elixir">iex&gt; Enum.uniq_by([%{<span class="hljs-symbol">x:</span> <span class="hljs-number">1</span>, <span class="hljs-symbol">y:</span> <span class="hljs-number">1</span>}, %{<span class="hljs-symbol">x:</span> <span class="hljs-number">2</span>, <span class="hljs-symbol">y:</span> <span class="hljs-number">1</span>}, %{<span class="hljs-symbol">x:</span> <span class="hljs-number">3</span>, <span class="hljs-symbol">y:</span> <span class="hljs-number">3</span>}], <span class="hljs-keyword">fn</span> coord -&gt; coord.y <span class="hljs-keyword">end</span>)
[%{<span class="hljs-symbol">x:</span> <span class="hljs-number">1</span>, <span class="hljs-symbol">y:</span> <span class="hljs-number">1</span>}, %{<span class="hljs-symbol">x:</span> <span class="hljs-number">3</span>, <span class="hljs-symbol">y:</span> <span class="hljs-number">3</span>}]
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="collections.html" class="navigation navigation-prev " aria-label="Previous page: 集合">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="pattern-matching.html" class="navigation navigation-next " aria-label="Next page: 模式匹配">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"version":"1.5.0","title":"Enum 模块","level":"1.4","depth":1,"next":{"title":"模式匹配","level":"1.5","depth":1,"path":"lessons/basics/pattern-matching.md","ref":"lessons/basics/pattern-matching.md","articles":[]},"previous":{"title":"集合","level":"1.3","depth":1,"path":"lessons/basics/collections.md","ref":"lessons/basics/collections.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"lessons/basics/enum.md","mtime":"2019-11-05T02:25:51.000Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-11-05T03:01:22.004Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

